package code

import (
	utils "gitee.com/ichub/goconfig/common/base/baseutils/stringutils"
	"gitee.com/ichub/gofactroy/common/ichubbase"
	"gitee.com/ichub/gofactroy/factroy/buildfactroy/rpc"
	"gitee.com/ichub/gofactroy/factroy/dbfactroy"
	"strings"
	"time"
)

type WebFactroy struct {
	rpcfactroy rpc.RpcFactroy
}

func (factroy *WebFactroy) BuildController(dbt *dbfactroy.DbFactroy) string {

	bs := dbfactroy.InstFileFactroy.ReadTemplateController()

	vars := make(map[string]interface{})

	vars["FileName"] = utils.Lcfirst(utils.Case2Camel(dbt.Table)) + "Controller.go"

	vars["Description"] = "服务" + utils.Case2Camel(dbt.Table) + "Controller"
	vars["Author"] = dbt.Author
	vars["DATETIME"] = time.Now().Format(ichubbase.FormatDateTime)

	vars["LcModel"] = utils.Lcfirst(utils.Case2Camel(dbt.Table))
	vars["Model"] = utils.Case2Camel(dbt.Table)
	vars["ESModelName"] = utils.Case2Camel(dbt.Table)

	vars["pkey"] = utils.Case2Camel(dbt.Pkey)
	vars["pkeyType"] = dbt.FindGoType(dbt.PkeyType)
	vars["PkeyValue"] = "0"
	vars["fmtType"] = "%d"
	if dbt.FindGoType(dbt.PkeyType) == "string" {
		vars["PkeyValue"] = "\"\""
		vars["fmtType"] = "%s"
	}

	return utils.ParseTemplateString(string(bs), vars)

}

func (factroy *WebFactroy) BuildRpcController(tableTool *dbfactroy.DbFactroy) string {

	bs := dbfactroy.InstFileFactroy.ReadTemplateRpcController()
	vars := make(map[string]interface{})

	vars["FileName"] = utils.Case2Camel(tableTool.Table) + "Controller.go"
	vars["Description"] = "Controller层" + utils.Case2Camel(tableTool.Table) + "Controller"
	vars["Author"] = tableTool.Author
	vars["DATETIME"] = time.Now().Format(ichubbase.FormatDateTime)

	vars["ModelName"] = utils.Case2Camel(tableTool.Table)
	vars["LcModelName"] = utils.Lcfirst(utils.Case2Camel(tableTool.Table))
	vars["TableName"] = tableTool.Table

	vars["pkeyField"] = tableTool.Pkey
	vars["pkey"] = utils.Case2Camel(tableTool.Pkey)
	vars["pkeyType"] = tableTool.FindProtoType(tableTool.PkeyType)
	pkeyType := tableTool.FindGoType(tableTool.PkeyType)
	if strings.Contains(pkeyType, "int64") {
		vars["Int64IdRemark"] = ""
		vars["Int32IdRemark"] = "// "
	} else {
		vars["Int64IdRemark"] = "// "
		vars["Int32IdRemark"] = ""
	}

	columns := tableTool.FindCOLUMNS()

	vars["Model2PbMsg"] = utils.List2string(factroy.rpcfactroy.Model2PbMsg(columns))
	vars["PbMsg2Model"] = utils.List2string(factroy.rpcfactroy.PbMsg2Model(columns))
	vars["IniPbMsg"] = utils.List2string(factroy.rpcfactroy.IniPbMsg(columns))

	vars["BuildWheres"] = utils.List2string(factroy.rpcfactroy.BuildIf(columns))
	return utils.ParseTemplateString(string(bs), vars)

}

func (factroy *WebFactroy) BuildControllerFile(dbt *dbfactroy.DbFactroy) (dir, f, c string) {

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	dir = dbfactroy.InstFileFactroy.TransDir(dbfactroy.Gendir_controller, fbase)

	f, c = fbase+"Controller.go", factroy.BuildController(dbt)
	dbfactroy.InstFileFactroy.WriteFileModule(dir, f, c)

	return
}

func (factroy *WebFactroy) BuildControllerRpcFile(dbt *dbfactroy.DbFactroy) (dir, f, c string) {

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))

	dir = dbfactroy.Gendir_rpcController
	f, c = fbase+"RpcController.go", factroy.BuildRpcController(dbt)
	dbfactroy.InstFileFactroy.WriteFileModuleRpc(dir, f, c)

	dbt.Dfcs = append(dbt.Dfcs, dbfactroy.DirFileContent{dir, f, c})

	return

}

func (factroy *WebFactroy) Build(dbt *dbfactroy.DbFactroy) (d, f, c string) {
	return factroy.BuildControllerFile(dbt)

}
func (factroy *WebFactroy) BuildRpc(dbt *dbfactroy.DbFactroy) (d, f, c string) {
	return factroy.BuildControllerRpcFile(dbt)

}
